<?php 
/**
 * 婚庆公司介绍
 * @author Administrator
 *
 */
class WeddingAction extends BaseAction
{
	public function index()
	{
				
			if ( $_GET['seek'] == 1 )
			{
			
				//搜索字段数组
				$field = array(
						'td_name',
						'td_id',	
				);
		
				$seek = common_html_Arr($_GET);
			
				//过滤空数组
				$seek = common_filtration($seek);
				
				//过滤非法字段
				foreach ( $seek as $k=>$v )
				{
						
					if ( !in_array($k, $field) )
					{
						unset($seek[$k]);
						continue;
					}
					
					
					
					$k == 'td_name' ? $seekStr .= "LOCATE('{$v}',{$k}) > 0  and " : $seekStr .= $k."='{$v}' and ";
						
				}
			//die($seekStr);
			}
			
			
				
			//载入分页扩展类
			import('ORG.Util.Page');
				
			//实例化model
			$WeddingModel = new WeddingModel();
			
			$count = $WeddingModel->getCount($seekStr);
			
			//实例化分页类
			$page = new Page($count, 8);
				
			//分页字符
			$pageStr = $page->firstRow.','.$page->listRows;
				
			$results = $WeddingModel->getListAll($pageStr, $seekStr);
			
			//die(P($results['add_time']));
			
			//数据重装
			foreach ( $results as $k=>&$v )
			{
				//时间格式转化
				$v['add_time'] = date('Y-m-d', $v['add_time']);
				//省略
				$v['td_text']=common_subtext($v['td_text'],40);

			}
			//die(P($results));
			//变量注入
			
			$this->results = $results;

			$this->show = $page->show();
			
			$this->display();
	}
	
	
	/**
	 * 更新（插入）视图
	 */
	public function update()
	{
	
		//更新
		if ( common_is_number($_GET['id']) )
		{
	
			$id = common_html_Arr($_GET['id']);
	
			//案列model
			$WeddingModel = new WeddingModel();
	
			//获取指定id数据
			$result = $WeddingModel->getRowById($id);
	
			
			//die(P($result));
	
			//图片转化
			$result['td_intro_img'] = json_decode($result['td_intro_img'], true);
	

			$this->result = $result;
	
			//die(P($result));
	
		}
						
		$this->display();
			
	}
	
	/**
	 *更新（插入）团队方法
	 */
	public function doUpdate()
	{
		
		IS_POST || die('非法操作');
		//参数过滤
		$requestArr = $_POST;
			
		//die(P($_POST));
		//校验厨师名
			if ( common_is_names($requestArr['td_name'], 2, 12) == NULL )
			{
				//响应状态
				$response['status'] = 0;
					
				//响应信息
				$response['info'] = '姓名格式错误';
					
				die(json_encode($response));
					
			}
			if (common_is_number($requestArr['sort']) == false)
			{
				$response['status'] = 0;
			
				$response['info'] = '排序参数错误';
			
				die(json_encode($response));
			}
		
		//图片校验
		$tmpArr = array_filter($requestArr['td_intro_img']);
			
		if ( count($tmpArr) <= 0 )
		{
	
			$response['status'] = 0;
	
			$response['info'] = '请至少上传一张图片';
	
			die(json_encode($response));
	
		}
			
			
		//图片转化
		$requestArr['td_intro_img'] = json_encode($requestArr['td_intro_img']);
			
		//实例化model
		$WeddingModel = new WeddingModel();
		//插入
		if ( common_is_number($requestArr['td_id']) == NULL )
		{
		//插入数据
			$result = $WeddingModel->insert($requestArr);
			
			if ( $result > 0 )
			{
				$response['status'] = 1;
				
				$response['info'] = '添加成功';
			}
			else 
			{
				$response['status'] = 0;
					
				$response['info'] = '添加失败';
			}
	
	
		}
		//更新
		else
		{
			//更新数据
			$result = $WeddingModel->update($requestArr, $requestArr['td_id']);
	
			if ( $result !== false )
			{
				$response['status'] = 1;
					
				$response['info'] = '修改成功';
			}
			else
			{
				$response['status'] = 0;
	
				$response['info'] = '修改失败';
			}
	
		}
			
		die(json_encode($response));
			
		
	}
	
	/**
	 * 上传头像图片处理方法
	 */
	public function uploadImg1()
	{
	
		//上传路径配置
		$path = C('IMG_UPLOAD_PATH').$_SESSION['console']['user_id'].'/';
	
		//上传文件
		$result = common_upload_img($path);
	
		//配置返回上传信息
		if ( !$result )
		{
			//错误信息
			$upInfo['status'] = 0;
			$upInfo['info'] = '上传文件失败';
	
		}
		else
		{
			//成功返回路径
			$upInfo['status'] = 1;
	
			$upInfo['path'] = substr($result[0]['savepath'].$result[0]['savename'], 1);
	
		}
	
	
		//返回上传信息
		die(json_encode($upInfo));
	
	}
	/**
	 * 上传图片处理方法
	 */
	public function uploadImg()
	{
	
		//上传路径配置
		$path = C('IMG_UPLOAD_PATH').$_SESSION['console']['user_id'].'/';
	
		//上传文件
		$result = common_upload_img($path);
	
		//配置返回上传信息
		if ( !$result )
		{
			//错误信息
			$upInfo['status'] = 0;
			$upInfo['info'] = '上传文件失败';
	
		}
		else
		{
			//成功返回路径
			$upInfo['status'] = 1;
	
			$upInfo['path'] = substr($result[0]['savepath'].$result[0]['savename'], 1);
	
		}
	
	
		//返回上传信息
		die(json_encode($upInfo));
	
	}
	/**
	 *删除控制
	 */
	public function del()
	{
			
		IS_AJAX || die('非法提交');
			
		$requset = common_html_Arr($_POST);
			
		//WeddingModel model
		$WeddingModel = new WeddingModel();
			
		$result = $WeddingModel->del($requset['check']);
			
		if ( $result > 0 )
		{
			$response['status'] = 1;
	
			$response['info'] = '删除成功';
		}
		else
		{
	
			$response['status'] = 0;
	
			$response['info'] = '删除失败';
	
		}
			
		die(json_encode($response));
	}
}

?>